﻿package
{
	import flash.display.Sprite ;
	import flash.display.Loader ;
	import flash.net.URLRequest ;
	import flash.events.Event;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;

	public class Image extends Sprite
	{
		private var ldr:Loader ;
		private var urlRequest:URLRequest;
		private var _url:String;
		public function Image()
		{
			
		}
		public function set url(value:String):void
		{
			_url=value;
			ldr=new Loader();
			ldr.contentLoaderInfo.addEventListener(Event.INIT ,getImageProperty);
			ldr.contentLoaderInfo.addEventListener(Event.COMPLETE ,handleComplete);
			urlRequest=new URLRequest(_url);
			ldr.load(urlRequest);
		}
		
		private var wid:int;
		private var hgt:int;
		public function  dimension(w:int,h:int):void
		{
			wid=w;
			hgt=h;
		}
		public function set bitmap(bitmapData:Bitmap):void
		{
			bitmapData.smoothing=true;
			this.addChild(bitmapData);
		}
		private function getImageProperty(e:Event):void
		{
			var xRatio:Number=wid/ldr.width ;
			var yRatio:Number=hgt/ldr.height ;
			if(xRatio>yRatio)
			{
				ldr.width *= yRatio;
				ldr.height *=yRatio;
			}
			else
			{
				ldr.width *=xRatio;
				ldr.height *=xRatio;
			}
			ldr.x=(wid-ldr.width)/2;
			ldr.y=(hgt-ldr.height)/2;
		}
		
		private function handleComplete(event:Event):void
		{
			var bitmap:Bitmap=event.target.content;
			bitmap.smoothing=true;
			addChild(ldr);
		}
	}
}